package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.User;
import com.example.myweb.Dto.PollVoteRequestDTO;
import com.example.myweb.Service.Auth.PollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/polls")
public class PollController {

    @Autowired
    private PollService pollService;

    /**
     * 对一个投票进行投票
     * @param pollId 投票附件的ID
     */
    @PostMapping("/{pollId}/vote")
    public ResponseEntity<ApiResponse<Void>> vote(
            @PathVariable Long pollId,
            @Valid @RequestBody PollVoteRequestDTO request,
            @AuthenticationPrincipal User currentUser) {

        pollService.vote(pollId, request.getOptionIndex(), currentUser);
        return ResponseEntity.ok(ApiResponse.success("投票成功"));
    }
}